package com.hanserwei.community.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanserwei.community.entity.po.Building;
import com.hanserwei.community.entity.request.building.BuildingAddRequest;
import com.hanserwei.community.entity.request.building.BuildingQueryRequest;
import com.hanserwei.community.mapper.BuildingMapper;
import com.hanserwei.community.service.BuildingService;
import com.hanserwei.community.uitils.ThrowUtils;
import com.hanserwei.community.enums.ResponseCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {

    @Override
    public Page<Building> queryBuildingByPage(BuildingQueryRequest request) {
        Page<Building> page = new Page<>(request.getCurrent(), request.getPageSize());
        QueryWrapper<Building> queryWrapper = this.getQueryWrapper(request);
        Page<Building> buildingPage = page(page, queryWrapper);
        ThrowUtils.throwIf(buildingPage == null, ResponseCodeEnum.INVALID_PARAMETER, "查询失败！检查参数！");
        return buildingPage;
    }

    @Override
    public Page<Building> listByPage(Integer currentNumber, Integer pageSize) {
        Page<Building> page = new Page<>(currentNumber, pageSize);
        Page<Building> buildingPage = page(page, null);
        ThrowUtils.throwIf(buildingPage == null, ResponseCodeEnum.INVALID_PARAMETER, "查询失败！检查参数！");
        return buildingPage;
    }

    @Override
    public boolean addBuilding(BuildingAddRequest request) {
        Building building = new Building();
        BeanUtils.copyProperties(request, building);
        boolean result = save(building);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "添加失败！");
        return true;
    }

    @Override
    public boolean updateBuilding(BuildingAddRequest request, Integer id) {
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        // 查询是否存在
        Building building = getById(id);
        ThrowUtils.throwIf(building == null, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        BeanUtils.copyProperties(request, building);
        boolean result = updateById(building);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "更新失败！");
        return true;
    }

    @Override
    public QueryWrapper<Building> getQueryWrapper(BuildingQueryRequest request) {
        Integer id = request.getId();
        String numbers = request.getNumbers();
        String units = request.getUnits();
        String remarks = request.getRemarks();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null && id > 0, "id", id);
        queryWrapper.like(numbers != null, "numbers", numbers);
        queryWrapper.like(units != null, "units", units);
        queryWrapper.like(remarks != null, "remarks", remarks);
        queryWrapper.orderBy(sortField != null, !Objects.equals(sortOrder, "descend"), sortField);
        return queryWrapper;
    }
}
